﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Forms;

namespace 智信科技物流系统
{
    public class SqlHelper
    {
        string constr = "Data Source=win-jqms2k5-bjzw02.xincache.cn;Initial Catalog=host3124562;User Id=host3124562;Password=x9j4e2M7;";
        public DataTable SqlSelect(string str)      //无连接模式访问数据库，返回结果到一个Table中，执行查询操作
        {
            try
            {
                DataTable dt;
                using (SqlConnection conn = new SqlConnection(constr))
                {


                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = str;        //参数在这里传入
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    dt = ds.Tables[0];
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }
        /*暂时不用了
             public DataTable SqlRead(string str)      //无连接模式访问数据库，返回结果到一个Table中，执行查询操作
            {
                try
                {
                    DataTable dt=new DataTable();
                    using (SqlConnection conn = new SqlConnection(constr))
                    {


                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = str;        //参数在这里传入
                        SqlDataReader read = cmd.ExecuteReader();
                        while (read.Read())
                        {
                            dt.Rows.Add(read);
                            Application.DoEvents();
                        }
                    }
                    return dt;
                }
                catch (Exception ex)
                {
                    throw ex;

                }
            }
             */
        public int SqlNomQuery(string str)          //执行更新类操作，包括UpData，Delete，Insert
        {
            try
            {
                int rowCounnt = 0;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    SqlCommand cmd = conn.CreateCommand();
                    conn.Open();
                    cmd.CommandText = str;                       //传值
                    rowCounnt = cmd.ExecuteNonQuery();
                    conn.Close();

                }
                return rowCounnt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int SqlScalar(string str)         //执行验证查询操作
        {
            try
            {
                int flog = 0;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    SqlCommand cmd = conn.CreateCommand();

                    cmd.CommandText = str;
                    conn.Open();
                    flog = (int)cmd.ExecuteScalar();
                    conn.Close();
                }
                return flog;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}


